﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using Controller;

namespace Web
{
    public partial class EditVendorPOItems : System.Web.UI.Page
    {
        Helper helper = new Helper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //OrdersController oc = new OrdersController();
                //IList<OrderEntity> orderEntities = oc.findOrderEntitiesByStatus("vendor order");
                //OrderListGridView.DataSource = orderEntities;
                //OrderListGridView.DataBind();

                IList<VendorPOItemEntity> vendorPoItemEntities =
                    Session["VendorPoItemEntities"] as IList<VendorPOItemEntity>;
                OrderListGridView.DataSource = vendorPoItemEntities;
                OrderListGridView.DataBind();
                AlertLabel.Text = "";

            }
            this.PreRender += new EventHandler(UpdateFlashMessage);
        }

        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }

        protected void cbSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = (CheckBox)OrderListGridView.HeaderRow.FindControl("cbSelectAll");
            if (chk.Checked)
            {
                foreach (GridViewRow r in OrderListGridView.Rows)
                {
                    CheckBox chkrow = (CheckBox)r.FindControl("SelectOrderCheckBox");
                    chkrow.Checked = true;
                }
            }
            else
            {
                foreach (GridViewRow r in OrderListGridView.Rows)
                {
                    CheckBox chkrow = (CheckBox)r.FindControl("SelectOrderCheckBox");
                    chkrow.Checked = false;
                }
            }
        }

        protected void MakeVendorPOButton_Click(object sender, EventArgs e)
        {
            IList<OrderEntity> orderEntityEntities = new List<OrderEntity>();
            int count = 0;
            foreach (GridViewRow r in OrderListGridView.Rows)
            {
                CheckBox chkrow = (CheckBox)r.FindControl("SelectOrderCheckBox");
                if (chkrow.Checked == true)
                {
                    OrderEntity orderEntity = new OrderEntity();
                    orderEntity.ProductId = int.Parse(((HiddenField)r.FindControl("ProductIdHiddenField")).Value);
                    orderEntity.No = (r.Cells[1].Text).ToString();
                    orderEntity.Name = (r.Cells[2].Text).ToString();
                    orderEntity.Quantity = Convert.ToInt32(((TextBox) r.FindControl("QuantityTextBox")).Text);
                    orderEntity.NetPrice = Convert.ToDecimal(((TextBox)r.FindControl("NetPriceTextBox")).Text);
                    orderEntity.Amount = orderEntity.Quantity*orderEntity.NetPrice;
                    orderEntity.Id = int.Parse(((HiddenField)r.FindControl("OrderIdHiddenField")).Value);
                    orderEntityEntities.Add(orderEntity);
                    count++;

                }
            }
            if (count > 0)
            {
                Session["OrderEntities"] = orderEntityEntities;
                Response.Redirect("MakeVendorPO.aspx");
            }
            else
            {
               // ClientScript.RegisterStartupScript(this.Page.GetType(), "No selected!", "alert('Please select order(s).')", true);
                //AlertLabel.Text = "Please select order(s).";
                Session["FlashMessage"] = "Error: Please Select Order(s)";
                Session["FlashMessageType"] = "error";
            }
        }

       protected void OrderListGridView_RowCommand(object sender, GridViewCommandEventArgs e)
       {
           if(e.CommandName=="Delete")
           {
               int index = Convert.ToInt32(e.CommandArgument);
               //OrderListGridView.DeleteRow(index);
           }
       }

       protected void OrderListGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
       {
           // No need to implement code here

       }



    }
}